package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.CustomerStatementReportWriteRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.otsapplicationreportsdk.customerstatementreport.dto.BatchCreateRequest;
import com.epoch.app.otsapplicationreportsdk.customerstatementreport.dto.BatchCreateResponse;
import com.epoch.app.otsapplicationreportsdk.customerstatementreport.dto.BatchDeleteRequest;
import com.epoch.app.otsapplicationreportsdk.customerstatementreport.dto.BatchDeleteResponse;
import com.epoch.app.otsapplicationreportsdk.customerstatementreport.model.CustomerStatementReport;
import com.epoch.app.otsapplicationreportsdk.customerstatementreport.service.CustomerStatementReportService;
import com.epoch.app.otsapplicationreportsdk.model.dto.CustomerStatementReportSDO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/11/11 22:19
 */
@Component
public class CustomerStatementReportWriteRepositoryImpl implements CustomerStatementReportWriteRepository {

    Log log = Log.getLogger(CustomerStatementReportWriteRepositoryImpl.class);

    @Resource
    CustomerStatementReportService customerStatementReportService;

    @Override
    public String create(CustomerStatementReportSDO sdo) {
        CustomerStatementReport customerStatementReport = new CustomerStatementReport();
        BeanUtils.copyProperties(sdo,customerStatementReport);
        Map m = (Map)customerStatementReportService.create(customerStatementReport);
        Map result = (Map)m.get("result");
        return (String)result.get("lastInsertId");
    }

    @Override
    public Integer batchDelete(String salesOrganizationCode, String channelCode, String customerCode, String profitCenter, String yearMonth) {
        BatchDeleteRequest deleteRequest = new BatchDeleteRequest();
        //deleteRequest.setIds(ids);
        deleteRequest.setSalesOrganizationCode(salesOrganizationCode);
        //deleteRequest.setChannelCode(channelCode);
        deleteRequest.setCustomerCode(customerCode);
        //deleteRequest.setProfitCenter(profitCenter);
        deleteRequest.setYearMonth(yearMonth);
        BatchDeleteResponse response = customerStatementReportService.batchDelete(deleteRequest);
        log.info("CustomerStatementReportService_callback|batchDelete_req={}", JSON.toJSONString(deleteRequest));
        return response.getCount();
    }

    @Override
    public Integer batchCreate(List<CustomerStatementReportSDO> sdoList, String yearMonth){
        int success = 0;
        int batch = 0;
        List<CustomerStatementReport> reportList = new ArrayList<>();
        for(CustomerStatementReportSDO sdo:sdoList){
            batch++;
            CustomerStatementReport basic = new CustomerStatementReport();
            BeanUtils.copyProperties(sdo,basic);
//            if ("期初".equals(sdo.getDocName())) {
//                sdo.setInvoiceDate("");
//            }
            if ("总计".equals(basic.getDocName())) {
                basic.setInvoiceDate("99999999");//目的是页面展示排序符合预期
            }
            basic.setYearMonth(yearMonth);

            reportList.add(basic);
            if (batch%500==0){
                BatchCreateRequest request = new BatchCreateRequest();
                request.setReportList(reportList);
                BatchCreateResponse response = customerStatementReportService.batchCreate(request);
                success +=response.getCount();
                log.info("CustomerStatementReportService_callback|batchCreate_reportList_size={},response={}", reportList.size(), JSON.toJSONString(response));
                reportList.clear();
            }
        }
        if (reportList.size()>0){
            BatchCreateRequest request = new BatchCreateRequest();
            request.setReportList(reportList);
            BatchCreateResponse response = customerStatementReportService.batchCreate(request);
            success +=response.getCount();
            log.info("CustomerStatementReportService_callback|batchCreate2_reportList2_size={},response2={}", reportList.size(), JSON.toJSONString(response));
        }

        return success;
    }



}
